<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px"
    >
      <el-form-item label="折扣方案名字" prop="discountName">
        <el-input v-model="dataForm.discountName" placeholder="折扣方案名字"></el-input>
      </el-form-item>
      <el-form-item label="参加时段" prop="timeIntervalList">
        <el-checkbox-group v-model="dataForm.timeIntervalList">
          <el-checkbox v-for="time in timeIntervalList" :key="time.timeIntervalId" :label="time.timeIntervalId">
            {{ time.timeIntervalName }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="权限角色" prop="roleList">
        <el-checkbox-group v-model="dataForm.roleList">
          <el-checkbox v-for="role in roleList" :key="role.roleId" :label="role.roleId">{{
              role.roleName
            }}
          </el-checkbox>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="dataForm.status" label="1">启用</el-radio>
        <el-radio v-model="dataForm.status" label="2">停用</el-radio>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { listRole } from '@/api/system/role'
import { addOrEditActivityDiscount, getActivityDiscount } from '@/api/marketing/activity/discount'
import { listTimeInterval } from '@/api/operation/businessSetup/timeInterval'

export default {
  data() {
    return {
      timeIntervalList: [],
      roleList: [],
      visible: false,
      dataForm: {
        discountId: 0,
        discountName: '',
        sellerId: '',
        timeIntervalId: '',
        authRoleList: '',
        status: '1',
        discountMode: '',
        remark: '',
        timeIntervalList: [],
        roleList: []
      },
      dataRule: {
        discountName: [
          { required: true, message: '折扣方案名字不能为空', trigger: 'blur' }
        ],
        sellerId: [
          { required: true, message: '租户不能为空', trigger: 'blur' }
        ],
        timeIntervalList: [
          { required: true, message: '参加时段不能为空', trigger: 'blur' }
        ],
        roleList: [
          { required: true, message: '权限角色不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态(1启用2停用)不能为空', trigger: 'blur' }
        ],
        discountMode: [
          { required: true, message: '折扣方案不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      //参加时段
      listTimeInterval().then(response => {
        this.timeIntervalList = response.data
      })
      // 权限角色
      listRole().then(response => {
        this.roleList = response.rows
      })
      this.dataForm.discountId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.discountId) {
          getActivityDiscount(this.dataForm.discountId).then(response => {
            let status = response.data.status.toString()
            this.dataForm = response.data
            this.dataForm.status = status
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addOrEditActivityDiscount(this.dataForm).then(response => {
            this.$modal.msgSuccess(response.msg)
            this.visible = false
            this.$emit('refreshDataList')
          })
        }
      })
    }
  }
}
</script>
